<template>
  <div class="product-con1">
    <el-card class="box-card">
      <div class="clearfix" slot="header">
        <span><i class="iconfont icon-dingdan1"></i> 筛选搜索</span>
        <el-button style="float: right" type="primary" size="small" @click="onSubmit">查询</el-button>
        <el-button style="float: right" type="button" size="small" @click="onReset">重置</el-button>
      </div>
      <div class="boxcc">
        <el-form ref="form" :model="listQuery" label-width="100px" :inline='true' >
          <el-form-item label="商品名称：">
            <el-input v-model="listQuery.name" class="itme11"></el-input>
          </el-form-item>
          <el-form-item label="货号：" class="itme11">
            <el-input v-model="listQuery.product_sn" class="itme11"></el-input>
          </el-form-item>
          
          <el-form-item label="上架状态："  >
            <el-select v-model="listQuery.publish_status" placeholder="全部" class="itme11">            
                <el-option label="上架" value="1" ></el-option>
                <el-option label="下架" value="0"></el-option>
          </el-select>          
          </el-form-item>
          <el-form-item label="审核状态：" >
            <el-select v-model="listQuery.verify_status" placeholder="全部" class="itme11"> 
                <el-option label="未审核" value="0" ></el-option>
                <el-option label="已审核" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品品牌：" >
            <el-select v-model="listQuery.brand_id" placeholder="全部" class="itme11">
             <el-option v-for="itema1 in brandOptions" :label="itema1.name" :value="itema1.id" :key="itema1.id" ></el-option>
            </el-select> 
          </el-form-item>
            <el-form-item label="商品分类：" >
                <el-cascader v-model="listQuery.product_category_id" :options="spfllist" @change="handleChange"></el-cascader>
            </el-form-item>
        </el-form>
      </div>
    </el-card>
  




    <el-table :data="productList" border style="width: 100%" class="listcc" :cell-style="{textAlign:'center'}" :header-cell-style="{'text-align':'center'}">
      <el-table-column prop="id" label="编号" width="180"></el-table-column >
      <el-table-column label="商品图片" width="80">
        <template slot-scope="scope"><img :src="scope.row.pic" /></template>
      </el-table-column>
      <el-table-column prop="name" label="商品名称" width="180"></el-table-column>
      <el-table-column label="价格/货号" width="130">
         <template slot-scope="scope">
           <div>价格：{{scope.row.price}}</div>
           <div>货号：{{scope.row.product_sn}}</div>
          </template>
      
      </el-table-column>
      <el-table-column prop="sort" label="排序" width="80"></el-table-column>
      <el-table-column prop="sale" label="商品销量" width="80"></el-table-column>

      <el-table-column label="审核状态" width="80">
        <template slot-scope="scope">
           {{scope.row.verify_status | verify}}
          </template>
      </el-table-column>
      <el-table-column label="是否上架" width="100">
          <template slot-scope="scope">
            <div>上架：{{scope.row.publish_status | sf}}</div>
            <div>推荐：{{scope.row.recommand_status | sf}}</div>
            <div>新品：{{scope.row.new_status | sf}}</div>
          </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button  type="primary" size="mini"   @click="updateProduct(scope.row.id)">修改</el-button>
          <el-button type="danger" size="mini" @click="delProduct(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

  </div>
</template>

<script>
import {fetchProductList,fetchBrand,spfl,delProduct} from "@/api/product.js"
export default {
  name:'product',
  data:function(){
    return{
      productList:[],
      listQuery:{
        pageSize:5,
        pegaNum:1,
        name:null,
        product_sn:null,
        publish_status:null,
        verify_status:null,
        brand_id:null,
        product_category_id:null,
        },
        total:0,
        brandOptions:[],
        spfllist:[]
    }
  },
  methods:{
    getProductList(){
      fetchProductList(this.listQuery).then((res)=>{
        console.log(res);
        this.productList = res.product;
        this.total=res.total
      })
    },
    getBrand(){
      fetchBrand().then((res)=>{
        console.log(res);
        this.brandOptions=res.data 
      })
    },

    getspfl() {
      spfl().then((res) => {
        console.log(res);
        var data = res.data;
        //重组数据
        data.forEach((item) => {
          let children = [];
          //添加分类的子级数据
          item.children.forEach((i) => {
            children.push({ value: i.id, label: i.name });
          });
          this.spfllist.push({value: item.id,label: item.name,children: children,});
        });
        console.log(this.spfllist);
      });
    },
    handleSizeChange(val) {
      console.log("修改页面条数的函数运行了", val);
      this.listQuery.pageSize = val;
      
      this.listQuery.pageNum = 1;
      //获取数据
      this.getProductList();
    },
    handleCurrentChange(val) {
      console.log("修改当前页的页码的函数运行了", val);
      //将页码信息 保存到  listQuery
      this.listQuery.pageNum = val;
      //再次获取数据
      this.getProductList();
    },
    handleChange(value) {
      console.log("级联菜单切换了", value);
      if (value !== null && value.length === 2) {
        this.listQuery.product_category_id = value[1];
      }
    },
    onSubmit(){
      console.log('查询按钮运行了');
      console.log(this.listQuery);
      this.getProductList()
    },
    onReset(){
      this.listQuery={
        pageSize:5,
        pegaNum:1,
        name:null,
        product_sn:null,

      }
      this.getProductList()
    },
    //跳转到 修改页面
    updateProduct: function (id) {
      console.log(id);
      this.$router.push("/pms/productUpdate/"+id);
    },
    //删除的函数
    delProduct: function (id) {
       //1.删除前需要提示
      this.$confirm("此操作将永久删除该条商品数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delProduct({ id: id }).then((res) => {
            console.log(res);
            if (res.type == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
                duration: 2000,
              });
              //2.删除后需要刷新数据
              //重新获取数据
              this.getProductList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
            duration: 2000,
          });
        });
    },
  },
  created(){
    this.getProductList();
    this.getBrand()
    this.getspfl()
  },
  filters:{
    verify(value){
      return value ? "已审核" : '未审核'
    },
    sf(value){
      return value ? "是" : '否'
    },
  },
}
</script>

<style lang='scss'>
.product-con1{
  padding: 10px;
  margin-bottom: 100px;
  .listcc{
    margin-top: 10px;
    
    img {
      width: 50px;
    }
  }
}
.item11{
  width: 150px;
}

</style>